//
//  XHRADViewController.m
//  BuDeJie
//
//  Created by haoran on 16/4/3.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "XHRADViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "XHRADItem.h"
#import <MJExtension/MJExtension.h>
#import <UIImageView+WebCache.h>
#import "XHRTabBarController.h"

#define iphone6P (XHRScreenH == 736)
#define iphone6 (XHRScreenH == 667)
#define iphone5 (XHRScreenH == 568)
#define iphone4 (XHRScreenH == 480)
#define XHRScreenH [UIScreen mainScreen].bounds.size.height
#define XHRScreenW [UIScreen mainScreen].bounds.size.width

#define XHRcode2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"



@interface XHRADViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;
@property (weak, nonatomic) IBOutlet UIView *adView;
/** 广告图片 **/
@property (nonatomic, weak) UIImageView *adImageView;
/** 模型 **/
@property (nonatomic, strong) XHRADItem *item ;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;
/** 定时器 **/
@property (nonatomic, weak) NSTimer *timer;
@end
/*
 占位视图实现:当一个界面,层次结构已经清晰,但是中间某一层位置,或者尺寸不确定,可以采用占位视图
 */
@implementation XHRADViewController

#pragma mark ---- 懒加载
-(UIImageView *)adImageView{
    if (_adImageView == nil) {
        UIImageView *imageView = [[UIImageView alloc]init];
        _adImageView = imageView;
        [self.adView addSubview:imageView];
        
        //打开用户交互功能
        imageView.userInteractionEnabled = YES;
        //添加点击事件
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [self.adImageView addGestureRecognizer:tap];
    }
    return _adImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1. 设置启动图片 -> 根据不同屏幕 加载不同图片 (屏幕适配)
    [self setupLaunchImage];
    
    //2.加载广告
    [self loadData];
    
    //3.创建定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}

#pragma mark ---- 加载广告
-(void)loadData{
    // 请求数据 -> 查看接口文档 -> 测试接口有没有问题 -> 解析数据(w_picurl,ori_curl:广告界面跳转地址,w,h) arr = dict[@"ad"]
    //1.创建请求对话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //参数字典
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"code2"]= XHRcode2;
    
//    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
    
    //2.发送网络请求
    [manager GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        // 解析数据 -> 写成plist文件 -> 字典转模型 -> 模型数据展示界面
        
        //获取广告字典数据
        NSDictionary *adDict = [responseObject[@"ad"] firstObject];
        
        //字典转模型
        XHRADItem * item = [XHRADItem mj_objectWithKeyValues:adDict];
        self.item = item;
        
        //展示界面 考虑图片的宽高和屏幕不一定匹配,需要对宽高做比例缩放
        CGFloat w = XHRScreenW;
        CGFloat h = XHRScreenW / item.w * item.h;
        self.adImageView.frame = CGRectMake(0, 0, w, h);
        [self.adImageView sd_setImageWithURL:[NSURL URLWithString:item.w_picurl]];
        
        
        
        //成功回调
//        NSLog(@"%@",responseObject);
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //失败回调
//        NSLog(@"%@",error);
    }];;
    
    
}

#pragma mark ---- 点击广告图片跳转
-(void)tap {
    
    NSURL *url = [NSURL URLWithString:self.item.ori_curl];
    
    UIApplication *app = [UIApplication sharedApplication] ;
  
    if([app canOpenURL:url]){
        //跳转到广告链接
        [app openURL:url];
    }
    
    
}

#pragma mark ---- 每隔一秒计时
-(void)timeChange{
    static int i = 3;
    
    if (i <= 0) {
        //执行跳转
        [self jump:nil];
    }

    i--;
    
    //设置标题
    NSString *str = [NSString stringWithFormat:@"跳过(%d)",i];
    [self.jumpBtn setTitle:str forState:UIControlStateNormal];
}


#pragma mark ---- 跳转按钮
- (IBAction)jump:(id)sender {
    
    //跳转至主界面框架,更改窗口的子控制器
    [UIApplication sharedApplication].keyWindow.rootViewController = [[XHRTabBarController alloc]init];
    //销毁定时器
    [self.timer invalidate];
}


#pragma mark ---- 设置启动图片
-(void)setupLaunchImage{
    // 6P: 736
    // 6:   667
    // 5 : 568
    // 4 : 480
    // 根据不同屏幕高度 加载不同图片
    UIImage *image = nil;
    if (iphone6P) {
        
        image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
        
    }else if(iphone6){
        
        image = [UIImage imageNamed:@"LaunchImage-800-667h"];
        
    }else if(iphone5){
        
        image = [UIImage imageNamed:@"LaunchImage-700-568h"];
    
    }else if(iphone4){
        image = [UIImage imageNamed:@"LaunchImage-700"];
    }
        
    _launchImageView.image = image;
    
    
}

@end
